var common = require('../../common.js');
var app = getApp();
Page({
  data: {
      page: 1,
      items:[],
      loading: 0
  },
  onLoad: function (options) {
      this.dataLoad();
  },
  dataLoad: function () {
    this.setData({loading:1});
    wx.request({
      url: "https://kk.timemost.com/api/index/items",
      method: 'POST',
      data: {
        token:common.token,
        page: this.data.page,
        appid:wx.getAccountInfoSync().miniProgram.appId
      },
      success: res => {
        wx.stopPullDownRefresh();
        if (res.data.status == 1) {
          this.setData({loading:0});
          this.data.items = this.data.items.concat(res.data.data);
          this.data.page++;
          this.setData({items:this.data.items,page:this.data.page});
        }else if(res.data.status == 2){
          this.setData({loading:0});
          this.data.items = this.data.items.concat(res.data.data);
          this.data.page++;
          this.setData({items:this.data.items,page:this.data.page});
          this.setData({loading:2});
        }else if(res.data.status == 3){
          this.setData({loading:4});
        }
      }
    });
  },
  onPullDownRefresh:function(){
    this.setData({page:1,loading:0,items:[]});
    this.dataLoad();
  },
  onReachBottom:function(){
    if (this.data.loading == 1 || this.data.loading == 2) { return; }
    this.dataLoad();
  },
  onShareAppMessage: function () {
    var appInfo = app.globalData.appInfo;
    return {
      title: appInfo.shareTitle,
      path: 'pages/index/index',
      imageUrl:  appInfo.shareIcon
    }
  },
  openItem:function(option){
    wx.navigateTo({
      url: '../mall/detail?item_id=' + option.currentTarget.dataset.itemid,
    });
  },
  toBack:function(){
    wx.navigateBack({
      delta:1
    });
  }
})